package authorized
import (
	"net/http"
	"github.com/xinliangnote/go-gin-api/internal/code"
	"github.com/xinliangnote/go-gin-api/internal/pkg/core"
)
type authCheckVerifyCodeRequest struct {
	PhoneNumber string `json:"phone_number"` // 返回码
	VerifyCode 	string `json:"verify_code"`
}
type dataSt struct{
	token string `json:"token"`
}
type authCheckVerifyCodeResponse struct {
	Code int `json:"code"` // 返回码
	Message string `json:"message"`
	Data dataSt `json:"data"`
}

func (h *handler) CheckVerifyCode() core.HandlerFunc {
	return func(ctx core.Context) {

		req := new(authCheckVerifyCodeRequest)
		res := new(authCheckVerifyCodeResponse)

		if err := ctx.ShouldBindJSON(req); err != nil {
			ctx.AbortWithError(core.Error(
				http.StatusBadRequest,
				code.ParamBindError,
				code.Text(code.ParamBindError)).WithError(err),
			)
			return
		}
		
		token, err := h.service.LoginByVerifyCode(ctx.RequestContext(), req.PhoneNumber, req.VerifyCode)
		if err != nil{
			res.Message =  err.Error()
			res.Code = 400
		}else{
			res.Code = 200
			res.Message = req.PhoneNumber + "登陆成功"
			res.Data = dataSt{token:token}
		}
		ctx.Payload(res)
		
	}
}